package com.cpi.cornpp.unit.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.unit.dto.UnitDTO;
import com.cpi.cornpp.unit.exception.UnitException;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;

public class UnitDAO extends DBDAO{
	private static final String SQL_SELECT_UNIT = "select description, qty, app_default from units where unit_id = ?";
	public UnitDTO getUnit(String unitId)throws UnitException{
		PreparedStatement pstm = null;
		ResultSet rs = null;
		UnitDTO unitDTO = null;
		try{
				pstm = prepareSQL(SQL_SELECT_UNIT, dsName);
				pstm.setString(1, unitId);
				rs = executeQuery(pstm);
				if( rs.next() ){
					unitDTO = new UnitDTO();
					unitDTO.setId(unitId);
					unitDTO.setDescription(rs.getString(1));
					unitDTO.setQuantity(rs.getDouble(2));
					unitDTO.setAppDefault(rs.getBoolean(3));
				}
		}catch (PoolException poolException){
			UnitException unitException = null;
			poolException.printStackTrace(System.out);
			unitException = new UnitException (poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_SELECT);
			throw unitException;
		} catch(SQLException sqle){
			UnitException unitException = null;
			sqle.printStackTrace(System.out);
			unitException = new UnitException (sqle, CornPPException.LAYER_DAO,CornPPException.ACTION_SELECT);
			throw unitException;
		}finally {
			try{
				closeConnection(pstm.getConnection());
			}catch(Exception exception){
			}
		}
		return unitDTO;
	}
}